
import React from 'react';
import PropTypes from 'prop-types';
import {injectIntl} from './intl';
import {Table} from 'antd';
import {conventEnumValueToString, formatAmount} from '../utils';
import {currency, transportType} from '../Enum';
import {TABLE} from '../constants';
import styles from './style.css';
/* eslint-disable react/prefer-stateless-function */
const rowKey = record => `${record.partSaleOrderCode}${record.partCode}`;
class DetailTable extends React.PureComponent {
    render() {
        const details = this.props.data || [];
        const rowObj = {};
        const sortedData = details.sort((a, b) => {
            if(a.partSaleOrderCode > b.partSaleOrderCode) return 1;
            else if(a.partSaleOrderCode < b.partSaleOrderCode) return -1;
            return 0;
        });
        const newDetails = sortedData.map(item => {
            if(rowObj[item.partSaleOrderCode])
                return {
                    ...item,
                    rowSpan: 0,
                };
            const count = sortedData.filter(d => d.partSaleOrderCode === item.partSaleOrderCode);
            rowObj[item.partSaleOrderCode] = true;
            return {
                ...item,
                rowSpan: count.length
            };
        });
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.partSaleOrderCode',
                    defaultMessage: '销售订单编号'
                }),
                dataIndex: 'partSaleOrderCode',
                render: (value, row) => {
                    const obj = {
                        children: value,
                        props: {},
                    };
                    obj.props.rowSpan = row.rowSpan;
                    return obj;
                }
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.currency',
                    defaultMessage: '币种'
                }),
                dataIndex: 'currency',
                render: (text, record) => {
                    const obj = {
                        children: conventEnumValueToString(currency, text),
                        props: {},
                    };
                    obj.props.rowSpan = record.rowSpan;
                    return obj;
                }
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.transportType',
                    defaultMessage: '运输方式'
                }),
                dataIndex: 'transportType',

                render: (text, record) => {
                    const obj = {
                        children: conventEnumValueToString(transportType, text),
                        props: {},
                    };
                    obj.props.rowSpan = record.rowSpan;
                    return obj;
                }
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.partCode',
                    defaultMessage: '备件编号'
                }),
                dataIndex: 'partCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.oldPartCode',
                    defaultMessage: '被替换备件编号'
                }),
                dataIndex: 'oldPartCode',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.oldPartName',
                    defaultMessage: '被替换备件名称'
                }),
                dataIndex: 'oldPartName',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.quantity',
                    defaultMessage: '数量'
                }),
                dataIndex: 'quantity',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.price',
                    defaultMessage: '单价'
                }),
                dataIndex: 'price',
                className: styles.textAlign,
                render: text => formatAmount(text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'detailTable.column.totalAmount',
                    defaultMessage: '总价'
                }),
                dataIndex: 'totalAmount',
                className: styles.textAlign,
                render: text => formatAmount(text)
            }
        ];
        return (

            <Table key="table"
                className="white-space-nowrap"
                rowKey={rowKey}
                columns={columns}
                pagination={false}
                loading={this.props.loading}
                dataSource={newDetails}
                {...TABLE} />
        );
    }
}

DetailTable.defaultProps = {
    data: [],
};

DetailTable.propTypes = {
    data: PropTypes.array,
};
export default injectIntl(DetailTable);
